package br.com.mmsearch.beans;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.htmlcleaner.*;

public class FunctionsHTML {

	private CleanerProperties props;
	private HtmlSerializer serializer;
	private HtmlCleaner cleaner;
	private URL url;
	private TagNode node;
	
	public FunctionsHTML(String stringURL) {
		try {
			setUrl(new URL("http://www.imdb.com/title/"+stringURL+"/soundtrack"));
			setProps(new CleanerProperties());
			setSerializer(new SimpleHtmlSerializer(props));
			setCleaner(new HtmlCleaner());
		} catch (MalformedURLException e) {
			
		}
	}

	public CleanerProperties getProps() {
		return props;
	}
	public void setProps(CleanerProperties props) {
		this.props = props;
	}
	public HtmlSerializer getSerializer() {
		return serializer;
	}
	public void setSerializer(HtmlSerializer serializer) {
		this.serializer = serializer;
	}
	public HtmlCleaner getCleaner() {
		return cleaner;
	}
	public void setCleaner(HtmlCleaner cleaner) {
		this.cleaner = cleaner;
	}
	public URL getUrl() {
		return url;
	}
	public void setUrl(URL url) {
		this.url = url;
	}

	public TagNode getNode() {
		return node;
	}

	public void setNode(TagNode node) {
		this.node = node;
	}
	
	public String getPartOfHtml(String expressao){
		StringBuffer str = new StringBuffer();
		try {
			node = cleaner.clean(url);
			Object[] objetos = node.evaluateXPath(expressao);
			for (Object objeto : objetos) {
				node = (TagNode) objeto;
				str.append(serializer.getAsString(node));
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XPatherException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str.toString();
	}
}
